﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CCUiLMCopyLocationOrientationDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands;

public class CCUiLMCopyLocationOrientationDlg : TxForm
{
  private CCCoLMCopyLocationOrientationCmd m_command;
  private CCApLMCopyLocationOrientationApp m_application;
  private Label label1;
  private Button m_cancel;
  private TxObjGridCtrl m_LocationsList;
  private TxFrameEditBoxCtrl m_referenceLoc;
  private TxFrame m_transientRefLoc;
  private bool m_refLocOK;
  private Label label3;
  private Button m_ok_button;
  private Container components;

  public CCUiLMCopyLocationOrientationDlg(CCCoLMCopyLocationOrientationCmd command)
  {
    this.InitializeComponent();
    this.m_command = command;
    this.m_command.DialogIsOpen(true);
    this.m_application = new CCApLMCopyLocationOrientationApp();
    this.m_refLocOK = false;
    this.RegisterToUnloadDocumentEvent();
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CCUiLMCopyLocationOrientationDlg));
    this.label1 = new Label();
    this.m_cancel = new Button();
    this.m_LocationsList = new TxObjGridCtrl();
    this.label3 = new Label();
    this.m_referenceLoc = new TxFrameEditBoxCtrl();
    this.m_ok_button = new Button();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.label1, "label1");
    this.label1.Name = "label1";
    this.m_cancel.DialogResult = DialogResult.Cancel;
    componentResourceManager.ApplyResources((object) this.m_cancel, "m_cancel");
    this.m_cancel.Name = "m_cancel";
    this.m_cancel.Click += new EventHandler(this.m_cancel_Click);
    ((UserControl) this.m_LocationsList).BorderStyle = BorderStyle.FixedSingle;
    this.m_LocationsList.ChangeGeneralSelection = false;
    this.m_LocationsList.CurrentRow = -1;
    this.m_LocationsList.EnableMultipleSelection = true;
    this.m_LocationsList.EnableRecurringObjects = false;
    this.m_LocationsList.ListenToPick = false;
    componentResourceManager.ApplyResources((object) this.m_LocationsList, "m_LocationsList");
    ((Control) this.m_LocationsList).Name = "m_LocationsList";
    this.m_LocationsList.PickLevel = (TxPickLevel) 1;
    this.m_LocationsList.ValidatorType = (TxValidatorType) 16 /*0x10*/;
    this.m_LocationsList.Picked += new TxObjGridCtrl_PickedEventHandler(this.m_LocationsList_Picked);
    componentResourceManager.ApplyResources((object) this.label3, "label3");
    this.label3.Name = "label3";
    this.m_referenceLoc.EnableRelativeToWorkingFrameCoordinates = true;
    this.m_referenceLoc.ListenToPick = false;
    componentResourceManager.ApplyResources((object) this.m_referenceLoc, "m_referenceLoc");
    ((Control) this.m_referenceLoc).Name = "m_referenceLoc";
    this.m_referenceLoc.ValidFrameSet += new TxFrameEditBoxCtrl_ValidFrameSetEventHandler(this.m_referenceLoc_ValidFrameSet);
    this.m_referenceLoc.Picked += new TxFrameEditBoxCtrl_PickedEventHandler(this.m_referenceLoc_Picked);
    this.m_referenceLoc.InvalidFrameSet += new TxFrameEditBoxCtrl_InvalidFrameSetEventHandler(this.m_referenceLoc_InvalidFrameSet);
    componentResourceManager.ApplyResources((object) this.m_ok_button, "m_ok_button");
    this.m_ok_button.Name = "m_ok_button";
    this.m_ok_button.Click += new EventHandler(this.m_ok_button_Click);
    ((Form) this).AcceptButton = (IButtonControl) this.m_ok_button;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((Form) this).CancelButton = (IButtonControl) this.m_cancel;
    ((Control) this).Controls.Add((Control) this.m_ok_button);
    ((Control) this).Controls.Add((Control) this.m_referenceLoc);
    ((Control) this).Controls.Add((Control) this.label3);
    ((Control) this).Controls.Add((Control) this.m_LocationsList);
    ((Control) this).Controls.Add((Control) this.m_cancel);
    ((Control) this).Controls.Add((Control) this.label1);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedDialog;
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CCUiLMCopyLocationOrientationDlg);
    ((Form) this).Closed += new EventHandler(this.CCUiLMCopyLocationsDlg_Closed);
    ((Control) this).ResumeLayout(false);
  }

  internal void InitializeDialog()
  {
    this.InitLocationsList();
    TxObjectList locationsFromSelection = this.m_application.GetLocationsFromSelection();
    if (((Collection<ITxObject>) locationsFromSelection).Count > 0)
      this.AddLocationsToList(locationsFromSelection);
    this.EnableOkButton();
    this.m_LocationsList.ListenToPick = true;
    this.m_LocationsList.ChangeGeneralSelection = true;
    this.m_LocationsList.SetValidator((ITxValidator) new CCUiLMCopyLocationOrientationValidator());
    this.m_referenceLoc.ListenToPick = true;
    this.m_referenceLoc.Focus();
    ((Control) this.m_referenceLoc).Select();
  }

  private void CCUiLMCopyLocationsDlg_Closed(object sender, EventArgs e)
  {
    this.m_command.DialogIsOpen(false);
    this.m_referenceLoc.LoseFocus();
    this.m_LocationsList.LoseFocus();
    if (TxPhysicalRoot.op_Inequality(TxApplication.ActiveDocument.PhysicalRoot, (ITxObject) null) && TxFrame.op_Inequality(this.m_transientRefLoc, (ITxObject) null))
      this.m_transientRefLoc.Delete();
    this.UnRegisterFromUnloadDocumentEvent();
    TxApplication.RefreshDisplay();
  }

  private void InitLocationsList()
  {
    // ISSUE: unable to decompile the method.
  }

  private void AddLocationsToList(TxObjectList weldPoints)
  {
    foreach (ITxObject weldPoint in (Collection<ITxObject>) weldPoints)
    {
      string name = weldPoint.Name;
      this.m_LocationsList.AppendObject(weldPoint);
    }
  }

  private void m_LocationsList_Picked(object sender, TxObjGridCtrl_PickedEventArgs args)
  {
    ITxObject itxObject = args.Obj;
    ITxCompoundOperation op = itxObject as ITxCompoundOperation;
    ITxLocationOperation locationOperation = itxObject as ITxLocationOperation;
    if (op != null)
      this.AddLocationsToList(this.m_application.GetLocationsFromOperation((ITxOperation) op));
    this.EnableOkButton();
  }

  private void m_cancel_Click(object sender, EventArgs e) => ((Form) this).Close();

  private void EnableOkButton()
  {
    if (this.m_LocationsList.IsValid() && ((Collection<ITxObject>) this.m_LocationsList.Objects).Count != 0 && this.m_refLocOK)
      this.m_ok_button.Enabled = true;
    else
      this.m_ok_button.Enabled = false;
  }

  private void m_referenceLoc_ValidFrameSet(
    object sender,
    TxFrameEditBoxCtrl_ValidFrameSetEventArgs args)
  {
    this.m_refLocOK = true;
    TxTransformation refFrame = this.getRefFrame();
    if (TxFrame.op_Equality(this.m_transientRefLoc, (ITxObject) null) && TxTransformation.op_Inequality(refFrame, (TxTransformation) null))
      this.m_transientRefLoc = TxFrameEx.CreateFrame(new TxFrameCreationDataEx("second ref Frame", refFrame, true));
    else if (TxTransformation.op_Inequality(refFrame, (TxTransformation) null) && TxTransformation.op_Inequality(this.m_transientRefLoc.AbsoluteLocation, refFrame))
      this.m_transientRefLoc.AbsoluteLocation = refFrame;
    TxApplication.RefreshDisplay();
    this.EnableOkButton();
  }

  private void m_referenceLoc_InvalidFrameSet(
    object sender,
    TxFrameEditBoxCtrl_InvalidFrameSetEventArgs args)
  {
    this.m_refLocOK = false;
    this.EnableOkButton();
  }

  private TxTransformation getRefFrame()
  {
    try
    {
      return this.m_referenceLoc.GetLocation();
    }
    catch (Exception ex)
    {
      return (TxTransformation) null;
    }
  }

  private void m_referenceLoc_Picked(object sender, TxFrameEditBoxCtrl_PickedEventArgs args)
  {
    this.m_refLocOK = true;
    this.EnableOkButton();
  }

  private void RegisterToUnloadDocumentEvent()
  {
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
  }

  private void UnRegisterFromUnloadDocumentEvent()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
  }

  private void OnUnloadDocument(object sender, TxDocument_UnloadingEventArgs args)
  {
    ((Form) this).Close();
  }

  private void m_ok_button_Click(object sender, EventArgs e)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    this.m_application.CopyLocationOrientation(this.m_LocationsList.Objects, this.getRefFrame());
    Cursor.Current = current;
    ((Form) this).Close();
  }
}
